package com.lbs.jiepiaisi.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.method.ReplacementTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.base.BaseActivity;
import com.lbs.jiepiaisi.bean.CityEntity;
import com.lbs.jiepiaisi.style.citylist.CityListSelectActivity;
import com.lbs.jiepiaisi.style.citylist.bean.CityInfoBean;
import com.lbs.jiepiaisi.style.citylist.utils.CityListLoader;
import com.lbs.jiepiaisi.utils.ButtonUtil;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.IntentUtil;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * 时间：2017/11/1 15:57
 * 作用：违章查询
 */

public class WeiZhangChaXunActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener,View.OnClickListener{
    //车牌选择
    private Button chengshijiancheng_btn;
    //城市选择
    private TextView tv_chengshi;
    private ShiMingChengReceiver shiMingChengReceiver;
    //违章查询结果
    private Button btn_ok;
    //当前选择的城市的城市代码
    private String chengshidaima;
    //车牌号码
    private EditText edit_chepaihaoma;
    //发动机号
    private EditText edit_fadongjihao;
    private RelativeLayout rel_fadongjihao;
    //车架号码
    private EditText edit_chejiahaoma;
    private RelativeLayout rel_chejiahaoma;
    //小型车
    private CheckBox smallCK;
    //大型车
    private CheckBox bigCK;

    private CityEntity.Result.Item selCity;

    private ImageView iv_fadong;

    private ImageView iv_chejia;

    private ChengShiReceiver chengshiReceiver;

    List<CityEntity.Result.Item> letterToCity=new ArrayList<>();

    List<CityEntity.Result.Item> letterToCityTemp=new ArrayList<>();

    private CityEntity cityEntity;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isTemplate=true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weizhangchaxun);
        titleView.setText("违章查询");
        shiMingChengReceiver=new ShiMingChengReceiver();
        registerReceiver(shiMingChengReceiver, new IntentFilter(Constant.SHI_MING_CHENG));
        //获取城市数据
        initCityData();
        findView();
        setListener();
    }

    private void initCityData() {
//        chengshiReceiver=new ChengShiReceiver();
//        registerReceiver(chengshiReceiver, new IntentFilter(Constant.CHENG_SHI_XIN_XI));
//        ChengShiLieBiaoBiz chengShiLieBiaoBiz=new ChengShiLieBiaoBiz();
//        chengShiLieBiaoBiz.chengShiLieBiaoBiz();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(shiMingChengReceiver);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.ck_small:
                break;
            case R.id.ck_big:
                bigCK.setChecked(false);
                Toast.makeText(this,"暂不支持大型车",Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }

    }

    private void findView() {
        chengshijiancheng_btn= (Button) findViewById(R.id.chengshijiancheng_btn);
        tv_chengshi= (TextView) findViewById(R.id.tv_chengshi);
        btn_ok= (Button) findViewById(R.id.btn_ok);
        edit_chepaihaoma= (EditText) findViewById(R.id.edit_chepaihaoma);
        edit_fadongjihao= (EditText) findViewById(R.id.edit_fadongjihao);
        edit_chejiahaoma= (EditText) findViewById(R.id.edit_chejiahao);
        rel_fadongjihao= (RelativeLayout) findViewById(R.id.rel_engine);
        rel_chejiahaoma= (RelativeLayout) findViewById(R.id.rel_classa);
        iv_chejia = (ImageView)findViewById(R.id.iv_chejia);
        iv_fadong = (ImageView)findViewById(R.id.iv_fadong);

        iv_chejia.setOnClickListener(this);
        iv_fadong.setOnClickListener(this);

        smallCK = (CheckBox)findViewById(R.id.ck_small);
        smallCK.setChecked(true);
        smallCK.setEnabled(false);
        bigCK = (CheckBox)findViewById(R.id.ck_big);

    }

    private void setListener() {
        smallCK.setOnCheckedChangeListener(this);
        bigCK.setOnCheckedChangeListener(this);

        //限制只输入大写，自动小写转大写
        edit_chepaihaoma.setTransformationMethod(new ReplacementTransformationMethod() {
            @Override
            protected char[] getOriginal() {
                char[] aa = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
                return aa;
            }

            @Override
            protected char[] getReplacement() {
                char[] cc = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
                return cc;
            }
        });

        chengshijiancheng_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),ChengShiJianChengActivity.class);
                startActivityForResult(intent, 0);
            }
        });
        //城市选择
        tv_chengshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent=new Intent(getApplicationContext(),ChengShiLieBiaoActivity.class);
//                startActivityForResult(intent, 1);
                Intent intent = new Intent(WeiZhangChaXunActivity.this, CityListSelectActivity.class);
                startActivityForResult(intent, CityListSelectActivity.CITY_SELECT_RESULT_FRAG);
            }
        });

        //查询违章结果
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(edit_fadongjihao.getText().toString().length() != 6){
                    Toast.makeText(mContext, "发动机号输入有误", Toast.LENGTH_LONG).show();
                    return;
                }
                if(edit_chejiahaoma.getText().toString().length() != 6){
                    Toast.makeText(mContext, "车架号输入有误", Toast.LENGTH_LONG).show();
                    return;
                }
//                WeiZhangChaXunBiz weiZhangChaXunBiz=new WeiZhangChaXunBiz();
//                /**
//                 * 城市代码：city
//                 * 车牌号码：hphm
//                 * 号牌类型，默认02：hpzl
//                 * 发动机号：engineno
//                 * 车架号：classno
//                 */
//                //weiZhangChaXunBiz.weiZhangChaXunBiz(chengshidaima);
                 /**
                 * 城市代码：city
                 * 车牌号码：hphm
                 * 号牌类型，默认02：hpzl
                 * 发动机号：engineno
                 * 车架号：classno
                 */

                Intent intent=new Intent(getApplicationContext(),ChaXunJieGuoActivity.class);

                Log.e("TAG","城市代码："+chengshidaima);
                Log.e("TAG","车牌号码："+edit_chepaihaoma.toString().toUpperCase());
                Log.e("TAG","号牌类型:"+"02");
                Log.e("TAG","发动机号："+edit_fadongjihao.getText());


                intent.putExtra("city", chengshidaima);//城市代码

                intent.putExtra("hphm",chengshijiancheng_btn.getText().toString()+edit_chepaihaoma.getText().toString().toUpperCase());//车牌号码

                intent.putExtra("hpzl","02");//号牌类型

                intent.putExtra("engineno",edit_fadongjihao.getText().toString());//发动机号

                intent.putExtra("classno",edit_chejiahaoma.getText().toString()); //车架号码

                startActivity(intent);


            }
        });

    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        switch (requestCode){
//            case 0:
//                if(data != null) {
//                    Bundle b = data.getExtras();
//                    String str = b.getString("resultView");
//                    chengshijiancheng_btn.setText(str);
//                }
//                break;
//            default:
//                break;
//        }
//    }

    class ShiMingChengReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            chengshidaima=intent.getStringExtra(Constant.KEY_DATA2);
            selCity = (CityEntity.Result.Item)intent.getSerializableExtra(Constant.KEY_DATA3) ;
            tv_chengshi.setText(selCity.getCityName());
            if(selCity.getEngine() == 0){
                rel_fadongjihao.setVisibility(View.GONE);
            }else {
                String msg = "输入发动机号后"+selCity.getEngineNo() + "位";
                if(selCity.getEngineNo() == 0){
                    msg = "请输入完整发动机号";
                }
                edit_fadongjihao.setHint(msg);
            }
            if(selCity.getClassa() == 0){
                rel_chejiahaoma.setVisibility(View.GONE);
            }else {
                String msg = "输入车架号后"+selCity.getClassNo() + "位";
                if(selCity.getClassNo() == 0){
                    msg = "请输入完整车架号";
                }
                edit_chejiahaoma.setHint(msg);
            }
            Log.e("TAG","接收到选择的市了"+selCity.toString());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_chejia:
                if(ButtonUtil.isFastClick()){
                    IntentUtil.gotoActivity(mContext,CarPicActivity.class,false);
                }
                break;
            case R.id.iv_fadong:
                if(ButtonUtil.isFastClick()){
                    IntentUtil.gotoActivity(mContext,CarPicActivity.class,false);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CityListSelectActivity.CITY_SELECT_RESULT_FRAG) {
            if (resultCode == RESULT_OK) {
                if (data == null) {
                    return;
                }
                Bundle bundle = data.getExtras();

                CityInfoBean cityInfoBean = (CityInfoBean) bundle.getParcelable("cityinfo");

                if (null == cityInfoBean) {
                    return;
                }
                chengshijiancheng_btn.setText(cityInfoBean.getAbbr());
                chengshidaima=cityInfoBean.getCityCode();
                tv_chengshi.setText(cityInfoBean.getCityName());
                if(cityInfoBean.getEngine().equals("0")){
                    rel_fadongjihao.setVisibility(View.GONE);
                }else {
                    String msg = "输入发动机号后"+cityInfoBean.getEngineno() + "位";
                    if(cityInfoBean.getEngineno().equals("0")){
                        msg = "请输入完整发动机号";
                    }
                    edit_fadongjihao.setHint(msg);
                }
                if(cityInfoBean.getClassa().equals("0")){
                    rel_chejiahaoma.setVisibility(View.GONE);
                }else {
                    String msg = "输入车架号后"+cityInfoBean.getClassno() + "位";
                    if(cityInfoBean.getClassno().equals("0")){
                        msg = "请输入完整车架号";
                    }
                    edit_chejiahaoma.setHint(msg);
                }
            }
        }else{
            if(data != null){
                switch (requestCode){
                    case 0:
                        if(data != null) {
                            Bundle b = data.getExtras();
                            String str = b.getString("resultView");
                            chengshijiancheng_btn.setText(str);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }

    class ChengShiReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            cityEntity=(CityEntity) intent.getSerializableExtra(Constant.KEY_DATA);

            letterToCity = cityEntity.getResult().getItems();
            for(CityEntity.Result.Item item : letterToCity){
                letterToCityTemp.add(item);
                /**
                 * 预先加载一级列表显示 全国所有城市市的数据
                 */
                CityListLoader.getInstance().loadCityDataFromNet(WeiZhangChaXunActivity.this, letterToCity);
            }
            if(cityEntity.getStatus().equals("0")){

                Toast.makeText(mContext, "加载成功", Toast.LENGTH_SHORT).show();

                Log.e("TAG","加载成功！！");
            }
        }

    }

}
